package com.taxinfo.utils;

import com.alibaba.fastjson.JSON;
import com.taxinfo.pojo.dto.SubMatchingDto;
import com.taxinfo.pojo.dto.SystemSubMatchingBO;
import com.taxinfo.pojo.dto.SystemSubMatchingList;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * httpclient工具类
 *
 */
public class HttpClientUtil {
    static CloseableHttpClient client = null;

    static {
        client = HttpClients.createDefault();
    }


    /**
     * post 请求  带json 参数
     *
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String postJson(String url, HashMap<String, Object> params) throws Exception {
        HttpPost httpPost = new HttpPost();
        httpPost.setURI(new URI(url));
        String jsonString = JSON.toJSONString(params);
        StringEntity stringEntity = new StringEntity(jsonString, "utf-8");
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");//发送json数据需要设置contentType
        httpPost.setEntity(stringEntity);
        CloseableHttpResponse execute = client.execute(httpPost);
        int statusCode = execute.getStatusLine().getStatusCode();
        if (200 != statusCode) {
            return "";
        }
        return EntityUtils.toString(execute.getEntity(), "utf-8");
    }

    /**
     * post 请求  带json 参数
     *
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String postList(String url, SystemSubMatchingList subMatchingDtos) throws Exception {
        HttpPost httpPost = new HttpPost();
        httpPost.setURI(new URI(url));
        String jsonString = JSON.toJSONString(subMatchingDtos);
        StringEntity stringEntity = new StringEntity(jsonString, "utf-8");
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");//发送json数据需要设置contentType
        httpPost.setEntity(stringEntity);
        CloseableHttpResponse execute = client.execute(httpPost);
        int statusCode = execute.getStatusLine().getStatusCode();
        if (200 != statusCode) {
            return "";
        }
        return EntityUtils.toString(execute.getEntity(), "utf-8");
    }

    public static String get(String url, HashMap<String, Object> params) throws Exception {
        HttpGet httpGet = new HttpGet();
        Set<String> keySet = params.keySet();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(url).append("?t=").append(System.currentTimeMillis());
        for (String key : keySet) {
            stringBuffer.append("&").append(key).append("=").append(params.get(key));
        }
        httpGet.setURI(new URI(stringBuffer.toString()));
        CloseableHttpResponse execute = client.execute(httpGet);
        int statusCode = execute.getStatusLine().getStatusCode();
        if (200 != statusCode) {
            return "";
        }
        return EntityUtils.toString(execute.getEntity(), "utf-8");
    }
}
